package com.ranchuanyin.schoolcat.controller;

import com.ranchuanyin.schoolcat.service.CatUserFriendService;
import com.ranchuanyin.schoolcat.units.RestBean;
import com.ranchuanyin.schoolcat.vo.FriendVO;
import com.ranchuanyin.schoolcat.vo.GroupSetVo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/cat/message")
public class MessageController {
    @Resource
    CatUserFriendService catUserFriendService;

    @GetMapping("/getFriendList/{id}")
    public RestBean<ArrayList<GroupSetVo>> getFriendList(@PathVariable String id) {
        List<FriendVO> friendList = catUserFriendService.getFriendList(Long.valueOf(id));
        ArrayList<GroupSetVo> groupSetVos = new ArrayList<>();
        Map<String, List<FriendVO>> collect = friendList.stream().collect(Collectors.groupingBy(FriendVO::getGroupName));
        collect.forEach((key,value) -> {
            GroupSetVo groupSetVo = new GroupSetVo();
            groupSetVo.setGroupName(key);
            groupSetVo.setList(value);
            groupSetVos.add(groupSetVo);
        });
        return RestBean.success(groupSetVos);
    }
}
